package com.changhai.diabetes.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.Patient;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.SessionUser;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.service.SearchService;
import com.changhai.diabetes.util.Constants;
import com.opensymphony.xwork2.ActionContext;

/**
 * 检索
 * 
 * @author Andy
 * 
 */
public class SearchAction extends BasePaginationAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private SearchService searchService;
	private String select1;
	private String select2;
	private String select3;
	private String fromValue;
	private String toValue;

	public String execute() throws Exception {
		return super.execute();
	}

	public String searchIndex() {
		return this.query();
	}

	@Override
	protected Page searchQuery(QueryDTO queryDTO) {
		Map map = new HashMap(); 
		queryDTO.setConditions(map);
		return searchService.searchQuery(queryDTO);
	}
	
	public String search() throws IOException {
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		PrintWriter pw = ServletActionContext.getResponse().getWriter();
		String res = "0";
		Map<String, Object> map = new HashMap<String, Object>();

		SessionUser sUser = (SessionUser) ActionContext.getContext().getSession().get(Constants.SESSION_USER_KEY);	
		if(sUser!=null){
			if (sUser.getAdminType() == 0) {
				map.put("userId", sUser.getId());
			}
		}
		map.put("table", select1);
		map.put("column", select2);
		map.put("condition", select3);
		map.put("valuea", fromValue);
		map.put("valueb", toValue);
		List<Patient> list = searchService.getPatientIdsBySearch(map);
		if(list!=null && list.size()>0){
			JSONArray array = new JSONArray();
			array = JSONArray.fromObject(list);
			pw.write(array.toString());
		}else{
			pw.write(res);
		}
		return NONE;
	}

	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public String getSelect1() {
		return select1;
	}

	public void setSelect1(String select1) {
		this.select1 = select1;
	}

	public String getSelect2() {
		return select2;
	}

	public void setSelect2(String select2) {
		this.select2 = select2;
	}

	public String getSelect3() {
		return select3;
	}

	public void setSelect3(String select3) {
		this.select3 = select3;
	}

	public String getFromValue() {
		return fromValue;
	}

	public void setFromValue(String fromValue) {
		this.fromValue = fromValue;
	}

	public String getToValue() {
		return toValue;
	}

	public void setToValue(String toValue) {
		this.toValue = toValue;
	}
}
